﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace StockTrader.Platform.Charting
{
    public enum GraphDirection
    {
        DOWN,
        UP
    }

    public partial class TickChart : Form
    {
 
        public TickChart(string title)
        {
            InitializeComponent();
            this.Visible = true;
            this.Text = title;
            this.SetScrollState(1, true);
            this.AdjustFormScrollbars(true);
            this.AutoScroll = true;
            formGraphics = this.CreateGraphics();
            //formGraphics.PageScale = DateTime.Now.Ticks;
            InitValues();
        }
        Pen myPen = new Pen(Color.Yellow);
        Graphics formGraphics;
        List<PointF> points = new List<PointF>();
        PointF lastPoint;
        float XLimit;
        float YLimit;

        PointF zeroPoint;

        private void InitValues()
        {
            XLimit = formGraphics.VisibleClipBounds.Width;
            YLimit = formGraphics.VisibleClipBounds.Height;
            zeroPoint = new PointF((float)(XLimit * 0.05), YLimit - (float)(YLimit * 0.05));
            lastPoint = new PointF(zeroPoint.X, zeroPoint.Y);
            DrawAxis();
        }

        private void DrawAxis()
        {
            myPen.Color = Color.Black;
            // X-Axis
            formGraphics.DrawLine(myPen, zeroPoint, new PointF(XLimit, zeroPoint.Y));
            // Y-Axis
            formGraphics.DrawLine(myPen, zeroPoint, new PointF(zeroPoint.X, 0));

            // Draw 10 Price points on Y-Axis
            double price = 30;
            for (int i = 0; i < 10; i++)
            {
                PointF textPoint = new PointF(zeroPoint.X - 20, zeroPoint.Y - (i * 30));
                string priceString = price.ToString();
                // prcie string
                formGraphics.DrawString(priceString, new Font("Arial", 7, FontStyle.Bold), myPen.Brush, textPoint);
                // price mark
                PointF markPoint = new PointF(zeroPoint.X, zeroPoint.Y - (i * 30));
                myPen.Color = Color.Yellow;
                formGraphics.DrawLine(myPen, markPoint, markPoint + new SizeF(0, -2));
                myPen.Color = Color.Black;
                price += 0.5;
            }
        }
        public void DrawTickLine(PointF pNew)
        {
            myPen.Color = Color.White;
            pNew.X += zeroPoint.X + 10;
            float Ydecimal = pNew.Y - 33;
            pNew.Y = pNew.Y * 14 + Ydecimal * 300;
            points.Add(pNew);
            formGraphics.DrawLine(myPen, lastPoint, pNew);
            lastPoint.X = pNew.X;
            lastPoint.Y = pNew.Y;
            //myPen.Dispose();
            //formGraphics.Dispose();
        }

        public void DrawTradePoint(PointF tp, string tradeText, GraphDirection graphDir)
        {
            myPen.Color = Color.Black;
            tp.X += zeroPoint.X + 10;
            float Ydecimal = tp.Y - 33;
            tp.Y = tp.Y * 14 + Ydecimal * 300;

            // trade price mark point
            Point priceMarkPoint = new Point((int)tp.X, (int)tp.Y-1);
            formGraphics.DrawRectangle(myPen, new Rectangle(priceMarkPoint, new Size(2, 2)));



            int direction = 100;
            Color tradeColor = Color.Green;
            if (graphDir == GraphDirection.DOWN)
            {
                direction = -100;
                tradeColor = Color.Red;
            }

            myPen.Color = tradeColor;
            Point lineMarkEnd = new Point();
            lineMarkEnd = priceMarkPoint + new Size(0, direction);
            formGraphics.DrawLine(myPen, priceMarkPoint, lineMarkEnd);
            formGraphics.DrawString(tradeText, new Font("Arial", 7, FontStyle.Bold), myPen.Brush, lineMarkEnd);
        }

        private void Form1_Paint(object sender, PaintEventArgs pe)
        {
            // Declares the Graphics object and sets it to the Graphics object
            // supplied in the PaintEventArgs.
            Graphics g = pe.Graphics;
            // Insert code to paint the form here.
            InitValues();
            PointF pLast = zeroPoint;
            foreach (PointF p in points)
            {
                formGraphics.DrawLine(myPen, pLast, p);
                pLast = p;
            }
        }

        private void OnPaint(PaintEventArgs pe)
        {
            Form1_Paint(null, pe);
        }
        private void TickChart_Load(object sender, EventArgs e)
        {

        }
    }


}
